package cn.codeforfun.jfinalplugin.activemq.core;

import cn.codeforfun.jfinalplugin.activemq.exception.JFinalActiveMqException;
import com.jfinal.log.Log;
import com.jfinal.plugin.IPlugin;

import static cn.codeforfun.jfinalplugin.activemq.core.JFinalTopic.topicMap;


public class ActiveMqPlugin implements IPlugin {

//    public static Map<String, JFinalQueue> mqMap = new HashMap<>();

    private boolean isStarted = false;

    private static Log log = Log.getLog(ActiveMqPlugin.class);

    public ActiveMqPlugin addQueue(JFinalQueue queue) {
        if (JFinalQueue.queueMap.containsKey(queue.getQueueName())) {
            try {
                throw new JFinalActiveMqException("There is a same name queue.The queue name is " + queue.getQueueName());
            } catch (JFinalActiveMqException e) {
                e.printStackTrace();
            }
        } else {
            JFinalQueue.queueMap.put(queue.getQueueName(), queue);
        }
        return this;
    }

    public ActiveMqPlugin addTopic(JFinalTopic topic) {
        if (topicMap.containsKey(topic.getTopicName())) {
            try {
                throw new JFinalActiveMqException("There is a same name topic.The topic name is " + topic.getTopicName());
            } catch (JFinalActiveMqException e) {
                e.printStackTrace();
            }
        } else {
            topicMap.put(topic.getTopicName(), topic);
        }
        return this;
    }

    public boolean start() {
        if (!isStarted) {
            JFinalQueue.optQueue("start");
            JFinalTopic.optTopic("start");
            isStarted = true;
        }
        return isStarted;
        //
//        try {
//            Set<Map.Entry<String, JFinalQueue>> entries = queueMap.entrySet();
//            for (Map.Entry entry : entries) {
//                JFinalQueue queue = (JFinalQueue) entry.getValue();
//                if (queue.startQueue()) {
//                    log.debug("The queue has been started.The name is " + queue.getQueueName());
//                }
//            }
//            log.info("JFinal queue has been started.");
//        } catch (Exception e) {
//            log.info("JFinal queue start error.");
//            e.printStackTrace();
//        }
//        try {
//            Set<Map.Entry<String, JFinalTopic>> entries = topicMap.entrySet();
//            for (Map.Entry entry : entries) {
//                JFinalTopic topic = (JFinalTopic) entry.getValue();
//                if (topic.startTopic()) {
//                    log.debug("The topic has been started.The name is " + topic.getTopicName());
//                }
//            }
//            log.info("JFinal topic has been started.");
//        } catch (Exception e) {
//            log.info("JFinal topic start error.");
//            e.printStackTrace();
//        }
//        isStarted = true;
//        return true;
    }

    public boolean stop() {
        if (!isStarted) {
            JFinalQueue.optQueue("stop");
            JFinalTopic.optTopic("stop");
            isStarted = true;
        }
        return isStarted;

//        if (!isStarted) return true;
        //
//        try {
//            Set<Map.Entry<String, JFinalQueue>> entries = queueMap.entrySet();
//            for (Map.Entry entry : entries) {
//                JFinalQueue queue = (JFinalQueue) entry.getValue();
//                if (queue.stopQueue()) {
//                    log.debug("The queue has been stopped.The name is " + queue.getQueueName());
//                }
//            }
//            log.info("JFinal queue has been stopped");
//        } catch (Exception e) {
//            log.info("JFinal queue stop error.");
//            e.printStackTrace();
//        }
//        try {
//            Set<Map.Entry<String, JFinalTopic>> entries = topicMap.entrySet();
//            for (Map.Entry entry : entries) {
//                JFinalTopic topic = (JFinalTopic) entry.getValue();
//                if (topic.stopTopic()) {
//                    log.debug("The topic has been stopped.The name is " + topic.getTopicName());
//                }
//            }
//            log.info("JFinal topic has been stopped");
//        } catch (Exception e) {
//            log.info("JFinal topic stop error.");
//            e.printStackTrace();
//        }
//        isStarted = false;
//        return true;
    }
}
